package cn.itcast.controller;


import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController{


    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;
    @Autowired
    private ModuleService moduleService;


	@RequestMapping("/login")
	public String login(String email,String password) {
	    /*	    1、判断email和password是否为空
//        2、 select  * from pe_user where email=?? and password=??
        2、只是先根据邮箱查询用户  select  * from pe_user where email=??
         如果用户为空代表邮箱错误
         如果用户不为空，判断密码
                     如果密码不一样，代表密码错误
                     如果密码一样，代表登录成功，把用户放到session中*/
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            request.setAttribute("error","邮箱或者密码不能为空");
            return "forward:login.jsp";
        }
        /*User user =userService.findByEmail(email);
        if (user==null){
            request.setAttribute("error","邮箱不存在,请重新输入");
            return "forward:login.jsp";
        }else{
            String passwordStr = new Md5Hash(password,user.getEmail(),2).toString();
            if (!user.getPassword().equals(passwordStr)){
                request.setAttribute("error","密码错误,请重新输入");
                return "forward:login.jsp";
            }
        }*/
        //如果使用shiro 的三大步骤:1.创建令牌 ,2 .获取主题Subject 3.开始认证
        password = new Md5Hash(password,email,2).toString();
        UsernamePasswordToken token = new UsernamePasswordToken(email,password);

        Subject subject = SecurityUtils.getSubject();
        try {
            //开始认证
            subject.login(token);
        } catch (AuthenticationException e) {
            request.setAttribute("error","邮箱或密码错误,请重新输入");
            return "forward:login.jsp";
        }

        User user = (User) subject.getPrincipal();
        //动态获取登录人的菜单
        List<Module> moduleList=moduleService.findModuleBy(user);

        session.setAttribute("loginUser",user);
        session.setAttribute("modules",moduleList);
        return "home/main";
	}

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }




}


